<?php
namespace Api\Controller;
use Think\Controller;
class AddressController extends HomeController {
	// 获取收货地址列表
	public function get_address_list(){
		$token = I('request.token', 'htmlspecialchars');
		if(empty($token)){
			error("有参数为空");
		}
		$login_info = _check_login($token);
    	$user_info = _check_user_by_id($login_info['user_id']);
		
		$arr_data = array (
				'user_id' => $user_info['id'],
		);
		$data = M('addr')->where($arr_data)->order("is_default desc")->select();
		if ($data !== false)
		{
			success($data);
		}
		else
		{
			error('db error',2);
		}
	}
	// 设置默认地址
	/* public function set_address_default(){
		$token = I('request.token', 'htmlspecialchars');
		$id = I('request.id', 'htmlspecialchars');
		if(empty($token) || empty($id)){
			error("有参数为空");
		}
		$login_info = _check_login($token);
    	$user_info = _check_user_by_id($login_info['user_id']);
    	
		$addr_where = array (
			'id' => $id,
			'user_id' => $user_info['id'],
		);
		$arr_save = array (
			'is_default' => 1,
		);
		$result = M('addr')->where($addr_where)->save($arr_save);
		
		if ($result !== false)
		{
			success();
		}
		else
		{
			error('db error', 2);
		}
	} */
	// 获取收货地址
	public function get_address_info(){
		$token = I('request.token', 'htmlspecialchars');
		$id = I('request.id', 'htmlspecialchars');
		if(empty($token) || empty($id)){
			error("有参数为空");
		}
		$login_info = _check_login($token);
		$user_info = _check_user_by_id($login_info['user_id']);
		
		$addr_where = array (
				'id' => $id,
				'user_id' => $user_info['id'],
		);
		$address_info = M('addr')->where($addr_where)->find();
		
		if ($address_info)
		{
			success($address_info);
		}
		else
		{
			error('未获取到收获地址信息');
		}
	}
	// 修改收货地址
	public function edit_address(){
		$token = I('request.token', 'htmlspecialchars');
		$id = I('request.id', 'htmlspecialchars');
		$name = I('request.name', 'htmlspecialchars');
		$mobile = I('request.mobile', 'htmlspecialchars');
		$addr = I('request.addr', 'htmlspecialchars');
		$district = I('request.district', 'htmlspecialchars');
		$is_default = I('request.is_default', 'htmlspecialchars');
		if(empty($token) || empty($id)){
    		error("有参数为空");
    	}
    	$login_info = _check_login($token);
    	$user_info = _check_user_by_id($login_info['user_id']);
    	
    	$addrObj = M('addr');
    	$address_info = $addrObj->find($id);
    	if (empty($address_info)){
    		error("未获取到收货地址信息");
    	}
    	// 是否需要修改收货人姓名
    	if(!empty($name)){
    		$arr_save['name'] = $name;
    	}
    	// 是否需要修改手机号码
    	if(!empty($mobile)){
    		$arr_save['mobile'] = $mobile;
    	}
    	// 是否需要修改详细地址
    	if(!empty($addr)){
    		$arr_save['addr'] = $addr;
    	}
    	// 是否需要修改地市县（区）三级级联信息
    	if(!empty($district)){
    		$arr_save['district'] = $district;
    	}
    	// 是否需要修改是否默认
    	if(!empty($nickname)){
    		$arr_save['is_default'] = $is_default;
    	}
    	$addr_where = array (
    			'id' => $id,
    			'user_id' => $user_info['id'],
    	);
    	$result = $addrObj->where($addr_where)->save($arr_save);    	
    	if ($result !== false)
    	{
    		// 如果此处设置了默认收货地址，需要将其他的地址改为非默认
    		if($is_default == 1){
    			$addr_dafault_where = array (
    					'id' => array('neq',id),
    					'user_id' => $user_info['id'],
    			);
    			$addr_default_save['is_default'] = 0;
    			$addrObj->where($addr_dafault_where)->save($addr_default_save);
    		}
    		success();
    	}
    	else
    	{
    		error("修改失败");
    	}
	}
	// 添加收货地址
	public function add_address(){
		$token = I('request.token', 'htmlspecialchars');
		$name = I('request.name', 'htmlspecialchars');
		$mobile = I('request.mobile', 'htmlspecialchars');
		$addr = I('request.addr', 'htmlspecialchars');
		$district = I('request.district', 'htmlspecialchars');
		$is_default = I('request.is_default', 'htmlspecialchars');
		if(empty($token) || empty($name) || empty($mobile) || empty($addr) || empty($district) || empty($is_default)){
			error("有参数为空");
		}
		$login_info = _check_login($token);
		$user_info = _check_user_by_id($login_info['user_id']);
		
		$addrObj = M('addr');
		$arr_data = array (
			'user_id' => $user_info['id'],
			'name' => $name,
			'mobile' => $mobile,
			'addr' => $addr,
			'district' => $district,
			'is_default' => $is_default,				
		);
		$data = $addrObj->add($arr_data);
		if ($data !== false)
		{
			// 如果此处设置了默认收货地址，需要将其他的地址改为非默认
			if($is_default == 1){
				$addr_dafault_where = array (
						'id' => array('neq',$data['id']),
						'user_id' => $user_info['id'],
				);
				$addr_default_save['is_default'] = 0;
				$addrObj->where($addr_dafault_where)->save($addr_default_save);
			}
			success();
		}
		else
		{
			error('添加失败');
		}
	}
	// 删除收货地址
	public function del_address(){
		$token = I('request.token', 'htmlspecialchars');
		$id = I('request.id', 'htmlspecialchars');
		if(empty($token) || empty($id)){
			error("有参数为空");
		}
		$login_info = _check_login($token);
		$user_info = _check_user_by_id($login_info['user_id']);
		 
		$addrObj = M('addr');
		$address_info = $addrObj->delete($id);
		if($address_info!=false){
			success("删除成功");
		}else{
			error("删除失败");
		}
	}
}